

import 'dart:io';
import 'dart:typed_data';

import 'package:bot_toast/bot_toast.dart';
import 'package:dolqun_movies/Interface/Network.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:dolqun_movies/UI/Pages/MainPage.dart';
import 'package:dolqun_movies/UI/Pages/Models/AnimationKeysModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/CacheManagerModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/ColorThemeModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/MainModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/WechatApiModel.dart';
import 'package:dolqun_movies/UI/SplashScreen.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_cache_manager/CacheManager.dart';
import 'package:path_provider/path_provider.dart' as PathProvider;
import 'package:uuid/uuid.dart';
import 'UI/Pages/Models/MainPageAnimationModel.dart';
import 'package:wechat_kit/wechat_kit.dart' as wechatKit;

void main(){
  runApp(_buildApp());
}

class InitializeModel {
  String user;
  String dir;

  InitializeModel({this.user, this.dir});
}
Future<InitializeModel> getConfiguration() async {
  var model = InitializeModel();
  model.dir = (await PathProvider.getApplicationDocumentsDirectory()).path;
  var instance = await SharedPreferences.getInstance();
  instance.getString("user") ?? await instance.setString("user", Uuid().v4());
  model.user = instance.getString("user");
  return model;
}

Widget _buildApp(){
  final api = NetworkInterface(url: "https://d.dolqun.net");
  Widget buildingWidget = SplashScreen();

  var platform = "UNKNOWN";
  var key = <int>[];
  if (Platform.isAndroid) {platform = "ANDROID"; key = [ 148, 121, 207, 139, 193, 253, 66, 12, 169, 21, 77, 29, 143, 217, 151, 67 ];}
  if (Platform.isIOS) { platform = "IOS"; key = [ 43, 124, 88, 62, 12, 62, 135, 84, 104, 100, 253, 65, 167, 237, 209, 90 ];}
  if (Platform.isWindows) platform = "WINDOWS";
  if (Platform.isLinux) platform = "LINUX";
  if (Platform.isMacOS) platform = "MACOS";
  WidgetsFlutterBinding.ensureInitialized();
  Color textColor = Colors.white70;
  Color backgroundColor = Color(0xff010712);
  buildingWidget = ListTileTheme(
    child: buildingWidget,
    iconColor: Colors.blue,
    textColor: Colors.blue,
    style: ListTileStyle.list,
    dense: true,
  );
  buildingWidget = MaterialApp(
    home: buildingWidget,
    navigatorObservers: [
      BotToastNavigatorObserver()
    ],
    theme: ThemeData(
        platform: TargetPlatform.iOS,
        scaffoldBackgroundColor: backgroundColor,
        textTheme: TextTheme(
          bodyText1: TextStyle(
              color: textColor
          ),
          headline6: TextStyle(
            color: Colors.blue
          ),
          bodyText2: TextStyle(
            color: Colors.white.withOpacity(0.6)
          ),
        ),
      iconTheme: IconThemeData(
        color: Colors.blue
      ),
      buttonTheme: ButtonThemeData(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12.0)
        ),
        buttonColor: Colors.white.withOpacity(0.5)
      ),
      fontFamily: "ukij-cjk",
    ),
  );
  buildingWidget = BotToastInit(child: buildingWidget);
  buildingWidget = ModelProvider<MainModel>(
    child: buildingWidget,
    model: MainModel(api),
  );

  buildingWidget = ModelProvider<AnimationKeysModel>(
    child: buildingWidget,
    model: AnimationKeysModel(),
  );
  buildingWidget = ModelProvider<MainPageAnimationModel>(
    child: buildingWidget,
    model: MainPageAnimationModel(),
  );
  buildingWidget = ModelProvider<ColorThemeModel>(
    model: ColorThemeModel(
      backgroundColor: backgroundColor,
      textColor: textColor,
      maskColor: Colors.white.withOpacity(0.04),
    ),
    child: buildingWidget,
  );

  Widget widget = buildingWidget;
  buildingWidget = FutureBuilder<InitializeModel>(
    future: getConfiguration(),
    builder: (context, snapshot){
      if (snapshot.hasError) return Center(child: Text("Error: ${snapshot.error}}", style: TextStyle(fontSize: 12.0),),);
      if (!snapshot.hasData) return Center(child: CircularProgressIndicator(),);
      return ModelProvider<CacheManagerModel>(
        child: widget,
        model: CacheManagerModel(
            cacheManager: HlsCacheManager(
                platform: platform,
                appKey: Uint8List.fromList(key),
                dir: snapshot.data.dir,
                user: snapshot.data.user
            )
        ),
      );
    },
  );

  buildingWidget = ModelProvider<WechatApiModel>(
    child: buildingWidget,
    model: WechatApiModel(
      wechatApi: wechatKit.Wechat()
    )
  );

  return buildingWidget;
}
